import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../app_strings.dart';

class TokenInterceptor extends InterceptorsWrapper {
  @override
  onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? token = sharedPreferences.getString(AppStrings.SP_KEY_TOKEN);
    if (token != null) {
      options.headers[AppStrings.HTTP_HEADER_AUTH] =
          AppStrings.HTTP_HEADER_TOKEN_PREFIX + token;
    }
    handler.next(options);
  }

  @override
  onResponse(Response response, ResponseInterceptorHandler handler) {
    handler.next(response);
  }

  @override
  onError(DioError err, ErrorInterceptorHandler handler) {
    // print('请求异常: ' + err.toString());
    handler.next(err);
  }
}
